package com.zyy.meetObject2;

public class ToStringTest {
    public static void main(String[] args) {
        Users users = new Users("Tom",12);
        System.out.println(users.toString());//地址：com.zyy.meetObject2.Users@10f87f48
        System.out.println(users);//地址：com.zyy.meetObject2.Users@10f87f48
        //自定义的类，在没有重写Object类的toString()打印对象引用变量时，其实就调用了对象的toString

        String s1 = new String("hello");
        System.out.println(s1.toString());//hello
        //像String、file、Date或包装类的Object的子类，它们都重写了Object
        //类的toString()，在调用toString返回的是实体内容
    }
}
class Users{
    String name;
    int age;

    public Users() {
    }

    public Users(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //习惯上，开发中对于自定义的类，调用toString（）时返回
    //的是实体内容

    //手动实现
//    @Override
//    public String toString() {
//        return "User{ name = " + name + ", age = " + age + "}";
//    }

    @Override
    public String toString() {
        return "Users{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}